<?php include 'functions.php'; echo $html_doctype; ?>
<html>
<head>
<title> Scratch Resources | Learn!</title>
<?php
	echo $html_meta;
	echo $html_css;
	echo $html_scripts;
?>
</head>

<body>
<!-- HEADER -->
<div id="maincontainer">

	<?php include 'header.php';?>
	<div class="twocolumn_wide">
	
	<?php
	resdap_connect();
	
	//Read Scratch Galleries RSS Feeds, and update database if neccesary.
	
	//feeds[catid]=['categoryname', 'gallertid']
	$feeds[0] = 75785; //scripting
	$feeds[1] = 75784; //variables&lists
	$feeds[2] = 75787; //drawing&animation
	$feeds[3] = 75788; //Scratch website
	$feeds[4] = 75789; //Scratch program
	$feeds[5] = 75790; //Other
	
	foreach ($feeds as $catid => $galleryid) {
		//read feed
		$xml = simplexml_load_file('http://scratch.mit.edu/feeds/getRecentGalleryProjects/'.$galleryid.'/');
		foreach ($xml->channel->item as $item) {
			$title = $item->title;
			$link  = $item->link;
			$img   = $item->imagelink;
			$descr = $item->description;
			$descr = substr($descr, (strpos($descr, '/>')+2)); // ' /&gt;'
			
			$a = substr($link, 32); //remove the http://scratch.mit.edu/ and keep user/123456
			$creator = substr($a, 0, strpos($a,'/'));
			$projectid = substr($a, strpos($a,'/')+1);
			
			//Check if the project is already in the database
			$q = "SELECT * FROM res_tutprojects WHERE projectid=$projectid";
			$result = mysql_query($q);
			if(mysql_num_rows($result)==0) {
				//The project is not in the database, store it!
				dev_out("New project found: '$title' by $creator");
				$qValues = "'" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($creator) . "', '" . mysql_real_escape_string($descr) . "', '" . mysql_real_escape_string($projectid)  . "', '" . $catid ."');";
				$q =  "INSERT INTO res_tutprojects ( title, creator, description, projectid, catid) VALUES(" . $qValues;
				mysql_query($q) or die(mysql_error());
				dev_out("Saved '$title'.");
			} else {
				dev_out("Project '$title' already in database.");
			}		
		}
		//feed finished
	}	
	
	
	//Display results from the database
	
	echo '<div class="box_standard"><h2>Newest Movement Tutorials</h2>'."\n\n";
	
	$result = mysql_query("SELECT * FROM res_tutprojects WHERE category='movement'");
	while($row = mysql_fetch_array($result)) {
		echo '<div class="prjbox_small">'."\n".
			 '<img src="http://scratch.mit.edu/static/projects/'.$row['creator'].'/'.$row['projectid'].'_sm.png"></img>'."\n".
			 '<h3>'.$row['title'].'</h3>'."\n".
			 '<p>'."\n".
			 'by <a href="http://scratch.mit.edu/users/'.$row['creator'].'">'.$row['creator'].'</a><br>'."\n".
			 $row['description'].
			 '</p><br>'."\n".
			 '</div>'."\n\n";
	}
	echo '</div>';
	
	/*	<div class="box_standard">
	<h2>Scratch Tutorial Projects</h2>
	</div>*/
	resdap_disconnect();
	?>
	
	</div>
	
	<div class="twocolumn_small">
	
	<div class="box_tiny">
	<h2>Lawlz! it works! </h2>
	As you can probably read, we now have a working blue sidebar at this page. You probably understand that this text is pure rubbish and should ofcourse
	be replaces as soon as possible, commonly written in the form of ASAP. Well, you need text to check your layout right? Ok. I'M IN UR PHP MODDING UR RESOURCE PAGE!!!1!!
	</div>
	
	<div class="box_tiny">
	<h2>Lawlz! it works! </h2>
	As you can probably read, we now have a working blue sidebar at this page. You probably understand that this text is pure rubbish and should ofcourse
	be replaces as soon as possible, commonly written in the form of ASAP. Well, you need text to check your layout right? Ok. I'M IN UR PHP MODDING UR RESOURCE PAGE!!!1!!
	</div>
	
	</div>
	
	<?php include('footer.php'); ?>
</div>
</body>

</html>